local extension = Package:new("first_package")

extension.extensionName = "tangshui"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["first_package"] = "第一弹",
}

local tsp__yuanshu = General:new(extension, "tsp__yuanshu", "qun", 4)

local tsp_yongsi = fk.CreateTriggerSkill{
  name = "tsp_yongsi",
  events = {fk.BeforeTurnStart,fk.AskForCardUse,fk.AskForPeaches},
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if event == fk.BeforeTurnStart then
      return player:hasSkill(self) and target == player
    elseif event == fk.AskForCardUse then
      return player:hasSkill(self) and target == player and data.cardName == "jink"
    else
      return player:hasSkill(self) and player.phase == Player.NotActive and target == player
    end
  end,
  on_cost = function (self, event, target, player, data)
    local reject = player:getTableMark("@tsp_yongsi_mark")
    if event == fk.BeforeTurnStart then
      return table.contains(reject,"#yongsi_play") or player.room:askForSkillInvoke(player,self.name,nil,"#yongsi-rejectPlay")
    elseif event == fk.AskForCardUse then
      return table.contains(reject,"#yongsi_jink") or player.room:askForSkillInvoke(player,self.name,nil,"#yongsi-rejectJink")
    else
      return table.contains(reject,"#yongsi_peach") or player.room:askForSkillInvoke(player,self.name,nil,"#yongsi-rejectPeach")
    end
    
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local is_maxHandCard = table.every(player.room:getOtherPlayers(player),function (element, index, array)
      return player:getHandcardNum()>element:getHandcardNum()
    end) 
    local reject = player:getTableMark("@tsp_yongsi_mark")
    room:drawCards(player,3)
    local become_maxHandCard = not is_maxHandCard and table.every(player.room:getOtherPlayers(player),function (element, index, array)
      return player:getHandcardNum()>element:getHandcardNum()
    end) 
    if event == fk.BeforeTurnStart then
      if (not table.contains(reject,"#yongsi_play")) and become_maxHandCard then
        room:addTableMark(player,"@tsp_yongsi_mark","#yongsi_play")
      end
      player.room.logic:breakTurn()
      return true
    elseif event == fk.AskForCardUse then
      if (not table.contains(reject,"#yongsi_jink")) and become_maxHandCard then
        room:addTableMark(player,"@tsp_yongsi_mark","#yongsi_jink")
      end
      player.room:addPlayerMark(player,"cant_use_jink")
    else
      if  (not table.contains(reject,"#yongsi_peach")) and become_maxHandCard then
        room:addTableMark(player,"@tsp_yongsi_mark","#yongsi_peach")
      end
    player.room:setPlayerMark(player,"cant_use_peach",1)
    end
  end,
  refresh_events = {fk.CardEffectFinished,fk.CardEffectCancelledOut,fk.AskForPeachesDone},
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self) and (event == fk.AskForCardResponse or target == player)
  end,
  on_refresh = function (self, event, target, player, data)
    if event == fk.AskForPeachesDone then
      player.room:setPlayerMark(player,"cant_use_peach",0)
    else
      player.room:removePlayerMark(player,"cant_use_jink")
    end
  end
}
local tsp_yongsi_prohibit = fk.CreateProhibitSkill{
  name = "#tsp_yongsi_prohibit",
  prohibit_use = function(self, player, card)
    return (player:getMark("cant_use_jink")>0 and card.trueName == "jink") or(player:getMark("cant_use_peach")>0 and card.trueName == "peach")
  end,
}
tsp_yongsi:addRelatedSkill(tsp_yongsi_prohibit)
tsp__yuanshu:addSkill(tsp_yongsi)

local tsp_pizhi = fk.CreateTriggerSkill{
  name = "tsp_pizhi",
  events = {fk.TargetConfirmed},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player == target and data.card.trueName == "slash"
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askForDiscard(player,0,2,true,self.name,true,".","pizhi-discard")
    self.cost_data = cards;
    return #cards>0
  end,
  on_use = function (self, event, target, player, data)
    player.room:addPlayerMark(player,"pizhi-extra",#self.cost_data)
    data.additionalEffect = (data.additionalEffect or 0) + player:getMark("pizhi-extra");
  end
}
local tsp_pizhi_delay = fk.CreateTriggerSkill{
  name = "#tsp_pizhi_delay",
  events = {fk.CardUseFinished},
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player:getMark("pizhi-extra")>0 and data.card.trueName == "slash"
  end,
  on_trigger = function (self, event, target, player, data)
    local cardUser = player.room:getPlayerById(data.from)
    if player:getMark("pizhi-extra")>0 then
      while player:getMark("pizhi-extra")>0 do
        player.room:removePlayerMark(player,"pizhi-extra")
        if #cardUser:getCardIds("hej")>0 and cardUser:isAlive() then
          player.room:useVirtualCard("dismantlement",nil,player,{cardUser})
        else
          break
        end
      end
    end
  end
}

tsp_pizhi:addRelatedSkill(tsp_pizhi_delay)
tsp__yuanshu:addSkill(tsp_pizhi)


Fk:loadTranslationTable {
  ["tsp__yuanshu"] = "袁术",
  ["#tsp__yuanshu"] = "蠹殁安还",
  ["designer:tsp__yuanshu"] = "牧孖",
  ["tsp_yongsi_jink"] = "庸肆",
  ["tsp_yongsi"] = "庸肆",
  [":tsp_yongsi"] = "回合开始时，或当你需要使用【闪】时，或当你于回合外需要使用【桃】时，你可以拒绝执行之并摸三张牌，然后若你手牌数变为全场唯一最多，本局你于该时机仅能如此做。",
  ["#yongsi-rejectPeach"] = "庸肆：你可以拒绝使用【桃】并摸三张牌，若手牌最多则此后你只能此做",
  ["#yongsi-rejectJink"] = "庸肆：你可以拒绝使用【闪】并摸三张牌，若手牌最多则此后你只能此做",
  ["#yongsi-rejectPlay"] = "庸肆：你可以摸三张牌并跳过本回合，若手牌最多则此后你只能此做",
  ["#tsp_yongsi_prohibit"] = "庸肆",
  ["@tsp_yongsi_mark"] = "庸肆",
  ["#yongsi_play"] = "出",
  ["#yongsi_jink"] = "闪",
  ["#yongsi_peach"] = "桃",
  ["tsp_pizhi"] = "圮秩",
  [":tsp_pizhi"] = "当你成为【杀】的目标后，你可以弃置至多两张牌令此牌对你结算等量次，则此牌结算结束后，你视为对其使用等两张【过河拆桥】。",
  ["pizhi-discard"] = "圮秩：你可以弃牌使此【杀】牌对你额外结算等量次"
}
local tsp__dinggu = General:new(extension, "tsp__dinggu", "wu", 3)

local tsp_chenzhu = fk.CreateActiveSkill{
  name = "tsp_chenzhu",
  anim_type = "offensive",
  can_use = function (self, player, card, extra_data)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and #player:getCardIds("he") > 0
  end,
  target_num = 0,
  min_card_num = 1,
  max_card_num = 2,
  prompt = "#tsp_chenzhu-description",
  card_filter = function (self, to_select, selected, selected_targets)
    return #selected <  2
  end,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards1 = effect.cards
    room:recastCard(effect.cards,player,self.name)
    if player.dead then
      return
    end
    local targets = room:askForChoosePlayers(player,table.map(table.filter(room:getOtherPlayers(player),function (element, index, array)
      return #element:getCardIds("he")>0
    end),Util.IdMapper),1,1,"#tsp_chenzhu-choose_other_player",self.name,false)
    local cards2 = {}
    if targets and #targets>0 then
      local target = room:getPlayerById(targets[1])
      cards2 = room:askForCard(target,1,2,true,self.name,true,".","#tsp_chenzhu-other_choose_cards",self.name,false)
      room:recastCard(cards2,target,self.name)
    end
    if player.dead then return end
    local all_cards = {}
    for index, value in ipairs(cards1) do
      table.insert(all_cards,Fk:getCardById(value))
    end
    if cards2 ~= nil and #cards2 > 0 then
      for index, value in ipairs(cards2) do
        table.insert(all_cards,Fk:getCardById(value))
      end
    end
    if #all_cards <2 then return end
    local heart,diomand,club,spade = false,false,false,false
    for index, value in ipairs(all_cards) do
      if value.suit == Card.Heart then
        if heart then
          return 
        end
        heart = true
      elseif value.suit == Card.Club then
        if club then
          return 
        end
        club = true
      elseif value.suit == Card.Diamond then
        if diomand then
          return 
        end
        diomand = true
      elseif value.suit == Card.Spade then
        if spade then
          return 
        end
        spade = true
      end
    end
    --[[local slash = Fk:cloneCard("slash")
    if player:prohibitUse(slash) then return end
    local max_num = slash.skill:getMaxTargetNum(player, slash)
    local slash_targets = {}
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not player:isProhibited(p, slash) and player:inMyAttackRange(p) then
        table.insert(slash_targets, p.id)
      end
    end
    if #slash_targets == 0 or max_num == 0 then return end
    local tos = room:askForChoosePlayers(player, slash_targets, 1, max_num, "#tsp_chenzhu-use_slash", self.name, true)
    if tos and #tos > 0 then
      room:useVirtualCard("slash", nil, player, table.map(tos, function(id) return room:getPlayerById(id) end), self.name, true)
      if not player.dead and #room:getPlayerById(tos[1]):getCardIds("ej")>0 then
        local move_target = room:askForChoosePlayers(player,
          table.map(room:getOtherPlayers(room:getPlayerById(tos[1])),Util.IdMapper),1,1,"#tsp_chenzhu-move_target::"..tos[1],self.name,true)
        if move_target then
          room:askForMoveCardInBoard(player,room:getPlayerById(tos[1]),room:getPlayerById(move_target[1]),self.name,nil,room:getPlayerById(tos[1]))
        end
      end
    end]]
    local use = player.room:askForUseCard(player, "slash", "slash", "#tsp_chenzhu-use_slash", true,{bypass_times = true})
    if use then
      player.room:useCard(use)
      local tos = use.tos[1]
    end
  end
}

local tsp_lvjie = fk.CreateTriggerSkill{
  name = "tsp_lvjie",
  events = {fk.Damage,fk.AfterCardsMove},
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player,self.name,nil,"#tsp_lvjie-use")
  end,
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) then
      return false
    end
    if event == fk.Damage and player:getMark("lvjie_damage-round") == 0 then
      return target == player
    else
      if player:getMark("lvjie_cardMove-round") > 0 then
        return false
      end
      for _, move in ipairs(data) do
        if move.to and move.to == player.id and move.toArea == Player.Equip then
          return true
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"tsp_lvjie-using",1)
    player.room:useVirtualCard("bogus_flower",nil,player,{player},self.name)
    player.room:setPlayerMark(player,"tsp_lvjie-using",0)
    local history_num = 0
    if event == fk.Damage then
      history_num = player:getMark("@lvjie-maxCard1") or 0
      player.room:setPlayerMark(player,"lvjie_damage-round",1)
    else
      history_num = player:getMark("@lvjie-maxCard2") or 0
      player.room:setPlayerMark(player,"lvjie_cardMove-round",1)
    end
    if not player.dead then
      local card_num = player:getMark("tsp_lvjie_cardnum")
      print(card_num)
      
      if card_num > history_num then
        player.room:drawCards(player,1,self.name)
        if event == fk.Damage then
          player.room:setPlayerMark(player,"@lvjie-maxCard1",card_num)
        else
          player.room:setPlayerMark(player,"@lvjie-maxCard2",card_num)
        end
      end
      local can_move = false
      if event == fk.Damage then
        if player:getMark("@lvjie-maxCard1") == 3 then
          if player:getMark("lvjie_move1") == 0 then
            player.room:addPlayerMark(player, "lvjie_move1")
            can_move = true
          end
        end
      else
        if player:getMark("@lvjie-maxCard2") == 3 then
          if player:getMark("lvjie_move2") == 0 then
            player.room:addPlayerMark(player, "lvjie_move2")
            can_move = true
          end
        end
      end
      if can_move then
        local targets = player.room:askForChooseToMoveCardInBoard(player, "#tsp_lvjie-move", self.name, true, nil)
        if #targets ~= 0 then
          targets = table.map(targets, function(id) return player.room:getPlayerById(id) end)
          player.room:askForMoveCardInBoard(player, targets[1], targets[2], self.name)
        end
      end
    end
    player.room:setPlayerMark(player,"tsp_lvjie_cardnum",0)
  end,
  refresh_events = {fk.AfterCardsMove},
  can_refresh = function (self, event, target, player, data)
    if (not player:hasSkill(self)) or (player:getMark("tsp_lvjie-using") == nil or player:getMark("tsp_lvjie-using") == 0)  then
      return false
    end
    self.cost_data = {}
    for _, move in ipairs(data) do
      if move.to == player.id and move.toArea == player.Hand and move.skillName == "bogus_flower" and move.proposer == player.id then
        for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(self.cost_data, info.cardId)
        end
      end
    end
    if #self.cost_data > 0 then
      return true
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"tsp_lvjie_cardnum",#self.cost_data)
  end
}

tsp__dinggu:addSkill(tsp_chenzhu)
tsp__dinggu:addSkill(tsp_lvjie)

Fk:loadTranslationTable {
  ["tsp__dinggu"] = "丁固",
  ["#tsp__dinggu"] = "烈胆匡国",
  ["designer:tsp__dinggu"] = "qqqqq",
  ["tsp_chenzhu"] = "沉诛",
  [":tsp_chenzhu"] = "出牌阶段限一次，你可以重铸至多两张牌，然后你令一名其他角色也如此做，"..
                     "若你与其被重铸的牌花色均不同，则你可以使用一张【杀】。",
  ["#tsp_chenzhu-description"] = "发动 沉诛 ，重铸至多两张牌",
  ["#tsp_chenzhu-choose_other_player"] = "沉诛：是否选择一名其他角色，令其也可重铸至多两张牌",
  ["#tsp_chenzhu-other_choose_cards"] = "沉诛：你可以重铸至多两张牌",
  ["#tsp_chenzhu-use_slash"] = "沉诛：你可以使用一张【杀】",
  ["#tsp_chenzhu-move_target"] = "你可以移动 %dest 场上的一张牌",
  ["tsp_lvjie"] = "履阶",
  ["@lvjie-maxCard1"] = "履阶伤害",
  ["@lvjie-maxCard2"] = "履阶装备",
  ["#tsp_lvjie-use"] = "履阶：你可以视为使用一张【树上开花】",
  ["#tsp_lvjie-move"] = "履阶：你可以移动场上一张牌",
  [":tsp_lvjie"] = "每轮每项限一次：①你造成伤害后②装备牌置入你的装备区后，你可以视为使用一张【树上开花】，若你于此时机以此法获得的牌为此前唯一最多，你摸一张牌，"..
                   "然后若此时机是首次达到三张，你可移动场上一张牌。"
}

local tsp_chenzhu_old = fk.CreateActiveSkill{
  name = "tsp_chenzhu_old",
  anim_type = "offensive",
  can_use = function (self, player, card, extra_data)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and #player:getCardIds("he") > 0
  end,
  target_num = 0,
  min_card_num = 1,
  max_card_num = 2,
  prompt = "#tsp_chenzhu-description",
  card_filter = function (self, to_select, selected, selected_targets)
    return #selected <  2
  end,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards1 = effect.cards
    room:recastCard(effect.cards,player,self.name)
    if player.dead then
      return
    end
    local targets = room:askForChoosePlayers(player,table.map(room:getOtherPlayers(player),Util.IdMapper),1,1,"#tsp_chenzhu-choose_other_player")
    local cards2 = {}
    if targets and #targets>0 then
      local target = room:getPlayerById(targets[1])
      cards2 = room:askForCard(target,1,2,true,self.name,true,".","#tsp_chenzhu-other_choose_cards")
      room:recastCard(cards2,target,self.name)
    end
    if player.dead then return end
    local all_cards = {}
    for index, value in ipairs(cards1) do
      table.insert(all_cards,Fk:getCardById(value))
    end
    if cards2 ~= nil and #cards2 > 0 then
      for index, value in ipairs(cards2) do
        table.insert(all_cards,Fk:getCardById(value))
      end
    end
    if #all_cards <2 then return end
    local heart,diomand,club,spade = false,false,false,false
    for index, value in ipairs(all_cards) do
      if value.suit == Card.Heart then
        if heart then
          return 
        end
        heart = true
      elseif value.suit == Card.Club then
        if club then
          return 
        end
        club = true
      elseif value.suit == Card.Diamond then
        if diomand then
          return 
        end
        diomand = true
      elseif value.suit == Card.Spade then
        if spade then
          return 
        end
        spade = true
      end
    end
    local slash = Fk:cloneCard("slash")
    if player:prohibitUse(slash) then return end
    local max_num = slash.skill:getMaxTargetNum(player, slash)
    local slash_targets = {}
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not player:isProhibited(p, slash) and player:inMyAttackRange(p) then
        table.insert(slash_targets, p.id)
      end
    end
    if #slash_targets == 0 or max_num == 0 then return end
    local tos = room:askForChoosePlayers(player, slash_targets, 1, max_num, "#tsp_chenzhu-use_slash_viewAs", self.name, true)
    if tos and #tos > 0 then
      local use = room:useVirtualCard("slash", nil, player, table.map(tos, function(id) return room:getPlayerById(id) end), self.name, true)
      if not player.dead and #room:getPlayerById(tos[1]):getCardIds("ej")>0 and #all_cards == 4 and use.damageDealt > 0 then
        local move_target = room:askForChoosePlayers(player,
          table.map(room:getOtherPlayers(room:getPlayerById(tos[1])),Util.IdMapper),1,1,"#tsp_chenzhu-move_target::"..tos[1],self.name,true)
        if move_target then
          room:askForMoveCardInBoard(player,room:getPlayerById(tos[1]),room:getPlayerById(move_target[1]),self.name,nil,room:getPlayerById(tos[1]))
        end
      end
    end
    --[[local use = player.room:askForUseCard(player, "slash", "slash", "#tsp_chenzhu-use_slash", true,{bypass_times = true})
    if use then
      player.room:useCard(use)
      local tos = use.tos[1]
      if not player.dead and #room:getPlayerById(tos[1]):getCardIds("ej")>0 and #all_cards == 4 and use.damageDealt > 0 then
        local move_target = room:askForChoosePlayers(player,
          table.map(room:getOtherPlayers(room:getPlayerById(tos[1])),Util.IdMapper),1,1,"#tsp_chenzhu-move_target::"..tos[1],self.name,true)
        if move_target then
          room:askForMoveCardInBoard(player,room:getPlayerById(tos[1]),room:getPlayerById(move_target[1]),self.name,nil,room:getPlayerById(tos[1]))
        end
      end
    end]]
  end
}

local tsp_lvjie_old = fk.CreateTriggerSkill{
  name = "tsp_lvjie_old",
  events = {fk.Damage,fk.AfterCardsMove},
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player,self.name,nil,"#tsp_lvjie-use")
  end,
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) then
      return false
    end
    if event == fk.Damage then
      return target == player
    else
      for _, move in ipairs(data) do
        if move.to and move.to == player.id and move.toArea == Player.Equip then
          return true
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"tsp_lvjie-using",1)
    player.room:useVirtualCard("bogus_flower",nil,player,{player},self.name)
    player.room:setPlayerMark(player,"tsp_lvjie-using",0)
    local history_num = 0
    if event == fk.Damage then
      history_num = player:getMark("@lvjie-maxCard1") or 0
    else
      history_num = player:getMark("@lvjie-maxCard2") or 0
    end
    if not player.dead then
      local card_num = player:getMark("tsp_lvjie_cardnum")
      
      if card_num >= history_num then
        player.room:drawCards(player,1,self.name)
        if event == fk.Damage then
          player.room:setPlayerMark(player,"@lvjie-maxCard1",card_num)
        else
          player.room:setPlayerMark(player,"@lvjie-maxCard2",card_num)
        end
      end
      local can_move = player:getMark("@lvjie-maxCard1") == 3 and player:getMark("@lvjie-maxCard2") == 3
      if can_move then
        local targets = player.room:askForChooseToMoveCardInBoard(player, "#tsp_lvjie-move", self.name, true, nil)
        if #targets ~= 0 then
          targets = table.map(targets, function(id) return player.room:getPlayerById(id) end)
          player.room:askForMoveCardInBoard(player, targets[1], targets[2], self.name)
        end
      end
    end
    player.room:setPlayerMark(player,"tsp_lvjie_cardnum",0)
  end,
  refresh_events = {fk.AfterCardsMove},
  can_refresh = function (self, event, target, player, data)
    if (not player:hasSkill(self)) or (player:getMark("tsp_lvjie-using") == nil or player:getMark("tsp_lvjie-using") == 0)  then
      return false
    end
    self.cost_data = {}
    for _, move in ipairs(data) do
      if move.to == player.id and move.toArea == player.Hand and move.skillName == "bogus_flower" and move.proposer == player.id then
        for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(self.cost_data, info.cardId)
        end
      end
    end
    if #self.cost_data > 0 then
      return true
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"tsp_lvjie_cardnum",#self.cost_data)
  end
}
local tsp__dinggu_old = General:new(extension, "tsp__dinggu_old", "wu", 3)

tsp__dinggu_old:addSkill(tsp_chenzhu_old)
tsp__dinggu_old:addSkill(tsp_lvjie_old)

Fk:loadTranslationTable {
  ["tsp__dinggu_old"] = "老丁固",
  ["#tsp__dinggu_old"] = "烈胆匡国",
  ["designer:tsp__dinggu_old"] = "qqqqq",
  ["tsp_chenzhu_old"] = "沉诛",
  [":tsp_chenzhu_old"] = "出牌阶段限一次，你可以重铸至多两张牌，然后你可以令一名其他角色也如此做，"..
                     "若你与其被重铸的牌:花色各不相同，则你可以视为一张【杀】;共四张，你于此【杀】造成伤害后，可以移动此【杀】目标场上一张牌。",
  ["#tsp_chenzhu-use_slash_viewAs"] = "沉诛：你可以视为使用一张【杀】",
  ["tsp_lvjie_old"] = "履阶",
  [":tsp_lvjie_old"] = "①你造成伤害后②装备牌置入你的装备区后，你可以视为使用一张【树上开花】，若你于此时机以此法获得的牌为此前最多，你摸一张牌，"..
                   "然后若你发动过另一项且均以此【树上开花】摸过至少三张牌，你移动场上一张牌。"
}

local yuji = General:new(extension, "tsp__yuji", "qun",2,3,General.Female)

local ripo_chooseCard = fk.CreateActiveSkill{
  name = "tsp__ripo_chooseCard",
  expand_pile = "tsp__ripoyanzi",
  card_filter = function (self, to_select, selected, player)
    return not selected or #selected < Self:getMark("ripo_cardsNum") and Self:getPileNameOfId(to_select) == "tsp__ripoyanzi"
  end,
  card_num = function ()
    return Self:getMark("ripo_cardsNum")
  end,
  target_num = 0,
  target_filter = Util.FalseFunc
}

Fk:addSkill(ripo_chooseCard)

local ripo = fk.CreateTriggerSkill{
  name = "tsp__ripoyanzi",
  events = {
    fk.AfterCardsMove,fk.DamageInflicted,fk.GameStart,fk.EventAcquireSkill,fk.BeforeCardsMove,fk.HpChanged
  },
  expand_pile = "tsp__ripoyanzi",
  derived_piles = "tsp__ripoyanzi",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) then return end
    if event == fk.EventAcquireSkill and (target ~= player or data ~= self) then return false end 
    if event == fk.DamageInflicted then
      return target == player and #player:getPile("tsp__ripoyanzi") > 0
    elseif event == fk.GameStart or event == fk.EventAcquireSkill then
      local suits = {}
      for _,c in ipairs(player:getCardIds("h")) do
        local card = Fk:getCardById(c)
        table.insertIfNeed(suits,card.suit)
      end
      if #suits > player.hp then
        self.cost_data = {}
        self.cost_data.removeCardsNum = #suits - player.hp
        return true
      end
    elseif event == fk.AfterCardsMove then
      local suits = {}
      for _,c in ipairs(player:getCardIds("h")) do
        local card = Fk:getCardById(c)
        table.insertIfNeed(suits,card.suit)
      end
      if #suits > player.hp then
        self.cost_data = {}
        self.cost_data.removeCardsNum = #suits - player.hp
        return true
      end
    elseif event == fk.HpChanged then
      if player ~= target then return end
      local suits = {}
      for _,c in ipairs(player:getCardIds("h")) do
        local card = Fk:getCardById(c)
        table.insertIfNeed(suits,card.suit)
      end
      if #suits > player.hp then
        self.cost_data = {}
        self.cost_data.removeCardsNum = #suits - player.hp
        return true
      end
    else
      local total = 0
      local _data = {}
      for index, move in ipairs(data) do
        local num = 0
        if (move.moveReason == fk.ReasonGive or move.moveReason == fk.ReasonDiscard) and move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
              num = num + 1
            end
          end
        end
        if num ~= 0 then
          total = total + num
          table.insert(_data, {index, num})
        end
      end
      if #_data == 0 or total > #player:getPile("tsp__ripoyanzi") then return end
      self.cost_data = _data
      return true
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.DamageInflicted then
      return player.room:askForSkillInvoke(player, self.name)
    elseif event == fk.GameStart or event == fk.AfterCardsMove or event == fk.HpChanged or event == fk.EventAcquireSkill then
      local room = player.room
      local targets = table.filter(room:getOtherPlayers(player, false), function(p) return not p:isKongcheng() end)
      local num = self.cost_data.removeCardsNum
      local listNames = {"log_spade", "log_club", "log_heart", "log_diamond"}
      local listCards = {{}, {}, {}, {}}
      for _, id in ipairs(player.player_cards[Player.Hand]) do
        local suit = Fk:getCardById(id).suit
        if suit ~= Card.NoSuit and not player:prohibitDiscard(id) then
          table.insertIfNeed(listCards[suit], id)
        end
      end
      local choices = U.askForChooseCardList(room, player, listNames, listCards, num, num, self.name, "#ripoyanzi_chooseCard:::"..num, false, false)
      local merged = {}
      for _, i in ipairs(choices) do
          for _, value in ipairs(listCards[table.indexOf(listNames, i)]) do
              table.insert(merged, value)
          end
      end
      self.cost_data.removeCards = merged
      return #choices > 0
    elseif event == fk.BeforeCardsMove then
      return player.room:askForSkillInvoke(player, self.name)
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.DamageInflicted then
      room:loseHp(player,1,self.name)
      local times = player:getLostHp()
      if player.dead then return end
      while times>0 do
        local use = U.askForUseRealCard(room,player,player:getPile("tsp__ripoyanzi"),".",self.name,nil,{expand_pile = "tsp__ripoyanzi"})
        if player.dead then return end
        if not use then
          break
        end
        times = times- 1
      end
      return true
    elseif event == fk.GameStart or event == fk.AfterCardsMove or event == fk.HpChanged or event == fk.EventAcquireSkill then
      player:addToPile("tsp__ripoyanzi",self.cost_data.removeCards,true,self.name)
      return 
    elseif event == fk.BeforeCardsMove then
      if player.dead then return end
      local room = player.room
      local _data = self.cost_data
      for _, tab in ipairs(_data) do
        local index, num = tab[1], tab[2]
        room:setPlayerMark(player,"ripo_cardsNum",num)
        local _,dat = room:askForUseActiveSkill(player,"tsp__ripo_chooseCard","ripo_chooseToReplace",false)
        room:setPlayerMark(player,"ripo_cardsNum",0)
        if not _ or not dat then return false end
        local ids = dat.cards;
        if #ids == num then
          local moveInfo = {}
          for _, id in ipairs(ids) do
            local from = room:getCardArea(id)
            local info = {}
            info.cardId = id
            info.fromArea = from
            table.insertIfNeed(moveInfo, info)
            info.fromSpecialName = "tsp__ripoyanzi"
          end
          if #moveInfo == num then
            data[index].moveInfo = moveInfo
            data[index].specialName = "tsp__ripoyanzi"
            data[index].specialVisible = true
          end
        end
      end
      return
    end
  end
}
yuji:addSkill(ripo)

local feiyan = fk.CreateTriggerSkill{
  name = "tsp__feiyansandie",
  switch_skill_name = "tsp__feiyansandie",
  events = {fk.EventPhaseStart,fk.EventPhaseChanging},
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player:hasSkill(self) and player.phase > Player.Start and player.phase < Player.Finish and player == target
    else
      local loseCard = {}
      local end_id = player.room.logic:getEventsByRule(GameEvent.Phase,1,function (e)
        return true
      end,1)
      if not end_id or #end_id == 0 then return false end
      player.room.logic:getEventsByRule(GameEvent.MoveCards,1,function (e)
        for _, move in ipairs(e.data) do
          for _, info in ipairs(move.moveInfo) do
            if (move.from == player.id and (info.fromArea == Player.Hand or info.fromArea == Player.Equip)) or 
              (move.to == player.id and (move.toArea == Player.Hand or move.toArea == Player.Equip)) then
              table.insertIfNeed(loseCard,info.cardId)
            end
          end 
        end
      end,end_id[1].id)
      if player:getSwitchSkillState(self.switchSkillName) == 1 then
        local suits = {}
        table.forEach(loseCard,function (element, index, array)
          table.insertIfNeed(suits,Fk:getCardById(element).suit)
        end)
        if #suits < 4 then
          return false
        end
      else
        local slash = table.filter(loseCard,function (element, index, array)
          return Fk:getCardById(element).trueName == "slash"
        end)
        if #slash < 2 then
          return false
        end
      end
      return player:hasSkill(self) and data.to > Player.Start and data.to < Player.Finish and player == target
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      local use = U.askForUseVirtualCard(player.room,player,"sincere_treat",nil,self.name,nil,true,true,true,true,{},true)
      self.cost_data = use
      return use
    else
      return player.room:askForSkillInvoke(player,self.name,nil,"#tsp__feiyansandie_changePhase:::"..U.ConvertPhse(data.to))
    end
  end,
  on_use = function (self, event, target, player, data)
    if event == fk.EventPhaseStart then
      player.room:notifySkillInvoked(player,self.name,"control")
      player:broadcastSkillInvoke(self.name)
      player.room:setPlayerMark(player, MarkEnum.SwithSkillPreName.."tsp__feiyansandie", player:getSwitchSkillState(self.name, true))
      player:setSkillUseHistory(self.name, player:usedSkillTimes(self.name, Player.HistoryGame) - 1, Player.HistoryGame)
      player.room:useCard(self.cost_data)
      
    else
      player.room:notifySkillInvoked(player,self.name,"switch")
      player:broadcastSkillInvoke(self.name)
      player.room:sendLog{
        type = "#PhaseChanged",
        from = player.id,
        arg = Util.PhaseStrMapper(data.to),
        arg2 = U.ConvertPhse(data.from),
      }
      data.to = data.from
    end
  end
}

yuji:addSkill(feiyan)

Fk:loadTranslationTable{
  ["tsp__yuji"] = "虞美人",
  ["designer:tsp__yuji"] = "请死",
  ["#tsp__yuji"] = "剥褪的赤练",
  ["tsp__ripoyanzi"] = "日迫崦嵫",
  [":tsp__ripoyanzi"] = "妾至多拥有体力数种花色的手牌，妾须将多余花色牌移出游戏。"..
    "妾弃置或交出牌时，可弃置或交出移出牌代替之；"..
    "妾受到伤害时，可防止之并失去1点体力，然后可使用已损失体力数张移出牌。",
  ['#ripoyanzi_chooseCard'] = "请选择将 %arg 种花色的牌移出游戏",
  ["tsp__ripo_chooseCard"] = "日迫崦嵫",
  ["ripo_chooseToReplace"] = "请选择代替的牌",
  
  ["tsp__feiyansandie"] = "飞雁三迭",
  [":tsp__feiyansandie"] = "转换技，妾的主要阶段开始时，妾可视为使用一张无距离限制的【推心置腹】。"..
    "妾的阶段结束时，若此阶段妾得失过共计，阴:四种花色的牌;阳:两张【杀】，"..
    "则妾可用一种花色所有牌交换移除牌中一种花色所有牌并将下个阶段改为任意阶段。",
  ["#tsp__feiyansandie_changePhase"] = "你可以发动 〖飞燕三叠〗 将%arg改为任意阶段"
}

return extension